package cn.shop.dys.util;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;

import java.util.List;

import cn.shop.dys.R;
/**
 *
 * 图片适配器
 * Created by sunjian on 2016/4/18.
 *
 */
public class MyAdapter extends CommonAdapter<String>
{

	/**
	 * 用户选择的图片，存储为图片的完整路径
	 */
//	public static List<String> mSelectedImage = new LinkedList<String>();
	private String mSelectedImagePath;
	/**
	 * 文件夹路径
	 */
	private String mDirPath;

	public MyAdapter(Context context, List<String> mDatas, int itemLayoutId,
					 String dirPath)
	{
		super(context, mDatas, itemLayoutId);
		this.mDirPath = dirPath;
	}
	ImageView[] mPreImageView = new ImageView[1];
	ImageView[] mPreSelect = new ImageView[1];
	@Override
	public void convert(final ImageViewHolder helper, final String item)
	{
		//设置no_pic
		helper.setImageResource(R.id.id_item_image, R.drawable.pictures_no);
		//设置no_selected
				helper.setImageResource(R.id.id_item_select,
						R.drawable.picture_unselected);
		//设置图片
		helper.setImageByUrl(R.id.id_item_image, mDirPath + "/" + item);
		
		final ImageView mImageView = helper.getView(R.id.id_item_image);
		final ImageView mSelect = helper.getView(R.id.id_item_select);
		mImageView.setColorFilter(null);
		//设置ImageView的点击事件
		mImageView.setOnClickListener(new OnClickListener()
		{
			//选择，则将图片变暗，反之则反之
			@Override
			public void onClick(View v)
			{

				// 已经选择过该图片
				if (mSelectedImagePath!=null)
				{
					if((mDirPath + "/" + item).equals(mSelectedImagePath)){
						mSelectedImagePath=null;
						mSelect.setImageResource(R.drawable.picture_unselected);
						mImageView.setColorFilter(null);
					}else {
						mPreSelect[0].setImageResource(R.drawable.picture_unselected);
						mPreImageView[0].setColorFilter(null);
						mSelectedImagePath=mDirPath + "/" + item;
						mSelect.setImageResource(R.drawable.pictures_selected);
						mImageView.setColorFilter(Color.parseColor("#77000000"));
						mPreSelect[0] =mSelect;
						mPreImageView[0] =mImageView;
					}
				} else
				// 未选择该图片
				{
					mSelectedImagePath=mDirPath + "/" + item;
					mSelect.setImageResource(R.drawable.pictures_selected);
					mImageView.setColorFilter(Color.parseColor("#77000000"));
					mPreSelect[0] =mSelect;
					mPreImageView[0] =mImageView;
					Log.d("test","11");
				}

			}
		});
		
		/**
		 * 已经选择过的图片，显示出选择过的效果
		 */
		/*if ((mDirPath + "/" + item).equals(mSelectedImagePath))
		{
			mSelect.setImageResource(R.drawable.pictures_selected);
			mImageView.setColorFilter(Color.parseColor("#77000000"));
		}*/
	}
	public String getSelectedImagePath(){
		return mSelectedImagePath;
	}
}
